package com.zaofan.mobile.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.Feedback;
import com.zaofan.bean.Message;
import com.zaofan.cache.CacheManage;
import com.zaofan.constant.UrlConstant;
import com.zaofan.service.FeedbackService;
import com.zaofan.service.MessageService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

/**
 * 系统公共接口
 *
 */
@WebServlet("/mobile/system")
public class SystemController extends BaseServlet {

	private static final long serialVersionUID = 5884947841079014459L;
	private FeedbackService feedbackService = new FeedbackService();
	private MessageService messageService = new MessageService();

	/**
	 * 版本检测接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void version(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			JsonUtils.writeSuccessJson("查询成功", CacheManage.version, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 系统反馈接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void feedback(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Feedback feedback = WebUtils.write2bean(request, Feedback.class);
			feedbackService.add(feedback);
			JsonUtils.writeSuccessJson("反馈成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("反馈失败", response);
		}
		JsonUtils.writeFailJson("反馈失败!!" + ",未知错误", response);
	}

	/**
	 * 系统消息接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void message(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			Integer begin = 0;
			Integer end = 10;
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			List<Message> messages = messageService.findMessage(begin, end);
			if (messages == null) {
				messages = new ArrayList<>();
			}
			for (Message message : messages) {
				message.setImage(UrlConstant.HOST_URL + message.getImage());
			}
			JsonUtils.writeSuccessJson("查询成功", messages, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败", response);
	}

}
